<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="用例负责人" prop="test_person">
        <el-select
          style="width: 100%;"
          v-model="optionsTestPersonId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getTestPersonInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in test_person_info"
            :key="index"
            :label="item.username"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属模块" prop="module">
        <el-select
          style="width: 100%;"
          v-model="optionsModuleId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入模块"
          :remote-method="getModuleInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用例标题" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="预置条件" prop="precondition">
        <el-input
          type="textarea"
          autosize
          v-model="ruleForm.precondition">
        </el-input>
      </el-form-item>

      <el-form-item label="测试数据" prop="test_data">
        <el-input autosize type="textarea" v-model="ruleForm.test_data"></el-input>
      </el-form-item>

      <el-form-item label="测试步骤" prop="test_steps">
        <el-input autosize type="textarea" v-model="ruleForm.test_steps"></el-input>
      </el-form-item>

      <el-form-item label="预期结果" prop="expected_results">
        <el-input autosize type="textarea" v-model="ruleForm.expected_results"></el-input>
      </el-form-item>


      <el-form-item label="用例属性" prop="property">
        <el-select v-model="ruleForm.property" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsProperty"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="重要级别" prop="level">
        <el-select v-model="ruleForm.level" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsLevel"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'Addcases',
    data() {
      return {

        EditUrl: this.$http.casesURL,  // get编辑的URL
        EditIdStr: 'cases_id',  // 模块id字符串
        pushObjName: 'cases',

        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsProperty: [], // 用例属性
        optionsLevel: [], // 重要级别
        optionsDescribe: [], // 测试结果

        // 其它表数据,搜索到的全部数据
        project_info: [],// 项目列表，获取1000条项目数据
        test_person_info: [],  // 用例负责人
        module_info: [],  // 用例所属模块

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsTestPersonId: [],
        optionsModuleId: [],

        ruleForm: {
          name: '',  //用例标题
          test_person: '',//用例负责人
          module: '',//所属模块
          precondition: '',//预置条件
          test_data: '',//测试数据
          test_steps: '',//测试步骤
          expected_results: '',//预期结果
          property: '', // 用例属性
          level: '', // 重要级别
          // describe: 3, // 测试结果
          status: '', //状态
          project: '',

          test_person_info: {},
          project_info: {},
          module_info: {},
        },  //
        rules: {
          name: [
            {required: true, message: '请输入用例标题', trigger: 'blur'},
            {min: 5, max: 100, message: '长度在 5 到 100 个字符', trigger: 'blur'}
          ],
          project: [
            {required: true, message: '请选择项目', trigger: 'change'}
          ],

          test_person: [
            {required: true, message: '用例负责人', trigger: 'change'}
          ],
          module: [
            {required: true, message: '所属模块', trigger: 'change'}
          ],
          precondition: [
            {required: false, message: '预置条件', trigger: 'blur'},
            {min: 0, max: 500, message: '长度在 0 到 500 个字符', trigger: 'blur'}
          ],
          test_data: [
            {required: false, message: '测试数据', trigger: 'blur'},
            {min: 0, max: 1000, message: '长度在 0 到 1000 个字符', trigger: 'blur'}
          ],
          test_steps: [
            {required: true, message: '测试步骤', trigger: 'blur'},
            {min: 0, max: 1000, message: '长度在 0 到 1000 个字符', trigger: 'blur'}
          ],
          expected_results: [
            {required: true, message: '预期结果', trigger: 'blur'},
            {min: 0, max: 1000, message: '长度在 0 到 1000 个字符', trigger: 'blur'}
          ],
          property: [
            {required: true, message: '用例属性', trigger: 'change'}
          ],
          level: [
            {required: true, message: '重要级别', trigger: 'change'}
          ],

          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common; // 获取标准CHOICES
      this.optionsProperty = this.getChoices.cases.property;  // 用例属性
      this.optionsLevel = this.getChoices.cases.level;  // 用例级别
      this.optionsDescribe = this.getChoices.cases.describe;  // 测试结果
    },
    methods: {
      submitForm() {
        let queryData = {
          inputObjName: '',
          module_project: this.optionsProjectId[0]
        };
        // cases_id
        if (this.optionsModuleId) {
          queryData['module_id'] = this.optionsModuleId[0]
        }
        let status = true;
        this.$http.getApiFunc(this.$http.getApiStatus)(this.$http.moduleURL, queryData)
          .then(res => {
            res.data.results.forEach(item => {
              if (item.id === this.optionsModuleId[0]) {
                status = false;
              }
            });
            if (status) {
              this.$alert(`项目下无此模块，请重新选择！`, '提示', {
                confirmButtonText: '确定'
              });
            } else {
              this.$myTool.submitForm(this);
            }
          })
          .catch(err => {
            console.log(err)
          });
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this);
      },

      // 用例所属人
      getTestPersonInfo(query) {
        this.$myTool.getObjList(this, this.$http.accountURL, this.test_person_info, query)
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },
      // 模块
      getModuleInfo(query) {
        let queryData = {
          inputObjName: query,
          module_project: this.optionsProjectId[0]
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, queryData)
      },

      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
    // 监听
    watch: {
      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },

      'ruleForm.test_person_info': function (value) {
        if (value) {
          this.optionsTestPersonId.push(value.id);
          this.test_person_info.push(value);
        } else {
          this.optionsTestPersonId = [];
          this.test_person_info = [];
        }
      },

      'ruleForm.module_info': function (value) {
        if (value) {
          this.optionsModuleId.push(value.id);
          this.module_info.push(value);
        } else {
          this.optionsModuleId = [];
          this.module_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value[0];
      },
      optionsTestPersonId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.test_person = value[0]
      },

      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },

  }
</script>


